﻿using System.Windows;
using Baosight.ZNH.ZMCommon;
using System;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI;
using Microsoft.Practices.Unity;
using System.Collections.Generic;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCB.UI.PopWindows;
using Baosight.Platform.Core.UI.Controls;

namespace Baosight.ZNH.ZMCB.UI
{
    /// <summary>
    /// 采区信息列表页面
    /// </summary>
    /// Title:ZMCB03
    /// 后台:ServiceZMCB03.java
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 高强
    /// Version: 1.0
    /// History:
    ///       2013-7-4  高强   [创建]
    ///  
    public partial class ZMCB03 : EFViewCmes
    {
        public ZMCB03()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.ma_grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 点击新增按钮，根据矿区形式判断是否可以新增矿井记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "采区信息-新增", "insert", null, sender, e);
        }

        /// <summary>
        /// 点击修改按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.ma_grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.ma_grid.GetCheckedEIRows();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");

            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);

            base.OnUpdateButtonClickPopup(eiInfo, "采区信息-修改", "update", null, sender, e);
        }

        /// <summary>
        /// 重写基类方法：根据数据的改变更新主页面的数据
        /// </summary>
        /// <param name="data"></param>
        public override void UpdateHomePageData(EIInfo data)
        {
            // 数据发布
            IEventAggregator eventAggreator = Container.Resolve<IEventAggregator>();
            EIDataPublishedEvent dataPublishedEvent = eventAggreator.GetEvent<EIDataPublishedEvent>();
            dataPublishedEvent.Publish(data);
        }

        /// <summary>
        /// 点击查看按钮执行的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;
            EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);

            base.OnHyperlinkButtonClickPopup(eiInfo, "采区信息-查看", "detail", null, sender, e);
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            AddMineAreaInfor mineAreaInfor = new AddMineAreaInfor(_dialogType);//构造一个新的修改页面
            mineAreaInfor.EIDataContext = new Platform.Core.UI.EIDataContext();

            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cb.parentInforMA"));//采区所属下拉框
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cb.IsStratified"));//是否分层下拉框
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cb.IsWings"));//是否两翼开采下拉框
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cb.IsFirstMA"));//是否为首采区
            mineAreaInfor.fdParentIdMA.Value = this.fdParentId.Value;
            mineAreaInfor.EIDataContext.Update(_eIInfo);
            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = mineAreaInfor;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 弹出窗口关闭后执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                this.ma_grid.Refresh();//刷新Grid区域
                this.UpdateHomePageData(new EIInfo());//发布数据
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        private void TabSource_SelectionChanged(object sender, RoutedEventArgs e)
        {
            if (TabSource.SelectedItem.Equals(this.roadWay))
            {
                Dictionary<string, string> _args = new Dictionary<string, string>();
                _args.Add("fdParentId", this.fdParentId.Value.ToString());
                _args.Add("fdParentType", this.fdParentType.Value.ToString());
                this.roadWay.LoadView<EFTabItem>("ZMCB05",_args);//加载巷道页面
            }
        }
    }
}
